package com.aion.jirc.irc;

import java.io.BufferedWriter;

/**
 * Created by IntelliJ IDEA.
 * Date: 11-02-16
 * Time: 21:11
 *
 * @author Aion
 */
public class IRCHandler extends OutputHandler {

    /**
     * Initializes the IRC handler.
     *
     * @param bw
     *          the <code>BufferedReader</code> instance
     * @throws Exception
     *          caught exceptions
     */
    protected IRCHandler(BufferedWriter bw) throws Exception {
        super(bw);
    }

    /**
     * Changes the nickname of the user to the given <code>String</code>.
     *
     * @param newNick
     *          the new nickname
     */
    public void changeNick(String newNick) {
        if (newNick != null && !newNick.isEmpty()) {
            send("NICK " + newNick);
        }
    }

    /**
     * Joins the given channel.
     *
     * @param channel
     *          the channel to join
     */
    public void join(String channel) {
        join(channel, "");
    }

    /**
     * Joins the given locked channel.
     *
     * @param channel
     *          the channel to join
     * @param key
     *          the authorization key
     */
    public void join(String channel, String key) {
        if (channel != null && !channel.isEmpty()) {
            if (!channel.startsWith("#")) {
                channel = "#" + channel;
            }
            send("JOIN " + channel + " :" + (key != null ? key : ""));
        }
    }

    /**
     * Parts the given channel.
     *
     * @param channel
     *          the channel to part
     */
    public void part(String channel) {
        part(channel, "");
    }

    /**
     * Parts the given channel.
     *
     * @param channel
     *          the channel to part
     * @param partMessage
     *          the part message
     */
    public void part(String channel, String partMessage) {
        if (channel != null && !channel.isEmpty()) {
            if (!channel.startsWith("#")) {
                channel = "#" + channel;
            }
            send("PART " + channel + " :" + (partMessage == null ? "" : partMessage));
        }
    }

    /**
     * Sends the given message to the given channel.
     *
     * @param channel
     *          the channel to send a message to
     * @param message
     *          the message to send
     */
    public void sendMessage(String channel, String message) {
        if (channel != null && !channel.isEmpty()) {
            if (message != null && !message.isEmpty()) {
                send("PRIVMSG " + channel + " :" + message);
            }
        }
    }

}
